package com.starsea.book.ui.newpage

import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.lxj.xpopup.XPopup
import com.starsea.book.R
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityCategoryDetailsBinding
import com.starsea.book.databinding.LayoutCommentListHeaderBinding
import com.starsea.book.ui.book.info.BookInfoViewModel
import com.starsea.book.ui.book.read.ReadBookActivity
import com.starsea.book.ui.newpage.adapter.CommentAllAdapter
import com.starsea.book.ui.newpage.pop.CommentPop
import com.starsea.book.ui.newpage.utils.loadNormalImage
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-01-11 20 GMT+8
 * @desc :
 */
class CommentListAllActivity : VMBaseActivity<ActivityCategoryDetailsBinding, BookInfoViewModel>() {

    override val binding by viewBinding(ActivityCategoryDetailsBinding::inflate)
    override val viewModel by viewModels<BookInfoViewModel>()

    lateinit var mAdapter: CommentAllAdapter

    var bookId: String = ""
    var commentType: Int = 1
    var parentId: String = ""
    var pageNo: Int = 1
    var headerBinding: LayoutCommentListHeaderBinding? = null
    var mPosition = -1

    override fun onActivityCreated(savedInstanceState: Bundle?) {

        bookId = intent.getStringExtra("bookId") ?: ""

        mAdapter = CommentAllAdapter(this) { value, type, position ->
            if (type == 2) {
                mPosition = position
                startActivity<CommentListChildActivity> {
                    putExtra("bookId", bookId)
                    putExtra("parentId", value.commentId)
                    putExtra("custName", value.custName)
                    putExtra("headUrl", value.headUrl)
                    putExtra("scoreNumber", value.scoreNumber)
                    putExtra("commentDesc", value.commentDesc)
                    putExtra("createTime", value.createTime)
                }
            } else if (type == 3) {
                viewModel.priseComment(
                    value.commentId ?: "",
                    "1",
                    if (value.likeFlag == 1) 2 else 1,
                    position
                )

            } else {
                mPosition = position
                LogUtils.e(">>> ", "mPosition = " + mPosition)
                reply("2", value.custName ?: "", value.commentId ?: "", position)
            }

        }


        viewModel.commentPriseResult.observe(this) {
            if (it.status == 200) {
                if (it.data != -1) {
                    if (it.likeFlag == 2) {
                        mAdapter.getItem(it.data ?: 0)!!.likeFlag = 0
                        mAdapter.getItem(it.data ?: 0)!!.likeCoount -= 1
                    } else {
                        mAdapter.getItem(it.data ?: 0)!!.likeFlag = 1
                        mAdapter.getItem(it.data ?: 0)!!.likeCoount += 1
                    }

                    mAdapter.notifyItemChanged((it.data ?: 0) + 1)
                } else {

                }

            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@CommentListAllActivity)
            } else {
                binding.srlRefresh.finishLoadMore()
//                mAdapter.addItems(it.data!!)
            }
        }

        binding.rvContent.layoutManager = LinearLayoutManager(this)

        binding.rvContent.adapter = mAdapter

        headerBinding =
            LayoutCommentListHeaderBinding.inflate(layoutInflater, binding.rvContent, false)

        mAdapter.addHeaderView {
            headerBinding!!
        }

        headerBinding!!.rtvComment.setOnClickListener {
            mPosition = -1
            reply("1", "", "", 0)
        }

        headerBinding!!.rgParent.setOnCheckedChangeListener { radioGroup, i ->
            pageNo = 1
            if (i == R.id.rb_all) {
                commentType = 1
            } else if (i == R.id.rb_new) {
                commentType = 2
            } else if (i == R.id.rb_long) {
                commentType = 3
            }
            getListData()
        }
        binding.srlRefresh.setOnRefreshListener {
            pageNo = 1
            getListData()
        }

        binding.srlRefresh.setOnLoadMoreListener {
            pageNo++
            getListData()
        }
        viewModel.getBookInfo(bookId)
        getListData()
        headerBinding!!.rtvRead.setOnClickListener {

            viewModel.saveBook(viewModel.bookData.value) {
//                startReadActivity(book)
                startActivity<ReadBookActivity> {
                    putExtra("bookUrl", bookId)
                    putExtra("inBookshelf", viewModel.inBookshelf)
                }
            }


        }


        viewModel.bookCommentResult.observe(this) {
            if (it.status == 200) {
                if (mPosition != -1) {
                    mAdapter.getItem(mPosition)!!.replyCount += 1
                    mAdapter.notifyItemChanged(mPosition + 1)
                } else {

                }
                pageNo = 1
                getListData()
                toastOnUi("评论成功")
//                mAdapter.setItems(it.data!!)
            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@CommentListAllActivity)
            } else {
                binding.srlRefresh.finishLoadMore()
//                mAdapter.addItems(it.data!!)
            }

        }

        viewModel.bookInfoResult.observe(this) {
            if (it.status == 200) {
                headerBinding!!.ivBookCover.loadNormalImage(this, it.data!!.bookCover ?: "")
                headerBinding!!.tvBookName.text = it.data!!.bookTitle
                headerBinding!!.tvAuthor.text = it.data!!.authorName ?: ""
                binding.titleBar.setTitle(it.data!!.bookTitle ?: "")

            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@CommentListAllActivity)
            } else {
                toastOnUi(it.msg)
            }
        }

        viewModel.commentListResult.observe(this) {
            if (it.status == 200) {
                if (pageNo == 1) {
                    binding.srlRefresh.finishRefresh()
                    if (pageNo == 1) {
                        mAdapter.setItems(it.data!!)
                    } else {
                        mAdapter.addItems(it.data!!)
                    }
                    binding.srlRefresh.finishLoadMore()
                    if (it.data!!.size < 10) {
                        binding.srlRefresh.finishLoadMoreWithNoMoreData()
                    }
                }
            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@CommentListAllActivity)
            } else {
                toastOnUi(it.msg)
            }
        }
    }

    private fun getListData() {
        viewModel.getCommentList(bookId, commentType.toString(), pageNo, 10, parentId)
    }


    fun reply(commentType: String, nickName: String, commentId: String, position: Int) {
        XPopup.Builder(this)
            .isDestroyOnDismiss(true)
            .asCustom(CommentPop(this, nickName, commentType) { content, score ->
                viewModel.replyComment(bookId, commentType, content, score, commentId)
            })
            .show()
    }

}